package cn.tedu.cart.controller

import org.apache.commons.lang.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

import cn.tedu.cart.service.CartService
import cn.tedu.common.pojo.Cart
import cn.tedu.common.pojo.SysResult

@RestController
@RequestMapping(Array("cart/manage"))
class CartController {
  
  @Autowired
  private var cartService: CartService = _

  @RequestMapping(Array("query"))
  def queryMyCart(userId: String) = if (StringUtils isNotEmpty userId) cartService queryMyCart userId else null

  //新增商品到购物车
  @RequestMapping(Array("save"))
  def saveCart(cart: Cart) = {
    try {
      cartService saveCart cart; SysResult.ok
    } catch {
      case t: Exception => t.printStackTrace(); SysResult.build(201, "", null)
    }
  }
  @RequestMapping(Array("update"))
  def updateCart(cart: Cart) = {
    try {
      cartService updateCart cart; SysResult.ok
    } catch {
      case t: Exception => t.printStackTrace(); SysResult.build(201, "", null)
    }
  }
  //删除
  @RequestMapping(Array("delete"))
  def deleteCart(cart: Cart) = {
    try {
      cartService deleteCart cart; SysResult.ok
    } catch {
      case t: Exception => t.printStackTrace(); SysResult.build(201, "", null)
    }
  }
}